/////
// paper_tables.do
// This file creates the tables in the main paper.
// The input file is localauthority_level.dta. 
/////

/////
// Table 3: Patronage and bureaucratic favoritism in hiring. 
/////

* open local authority-level data
use "localauthority_level.dta", clear 
est clear

* define dependant variable 
local depvar hired 
	
* model with ethnic group-locality and year fixed effects 
local indvar "c.majority##c.la_clerk##c.clerketh##c.perc`depvar'miss"
quietly reghdfe perc`depvar' `indvar', absorb(TERETHid year) vce(cluster territory_level) // ethnic group-locality and year fixed effects 
		est store model_perc`depvar'_m1
		estadd local TERETH "Yes"
		estadd local ETHYEAR "No"
		estadd local year "Yes"
		estadd local TERYEAR "No"
		* calculate linear combinations and other stats 
			* no clerk and j is non co-ethnic with the council
			summarize perc`depvar' if majority == 0 & la_clerk == 0 & clerketh == 0 
			estadd scalar noclerk_majnoteth = r(mean)				
			* no clerk and j is co-ethnic with the council
			est restore model_perc`depvar'_m1
			lincomest _b[majority] 
			estadd scalar noclerk_majeth = r(estimate)
			estadd scalar noclerk_majeth_se = r(se)
			est store m1_lincom1
			* j is non-coethnic with the clerk and the council
			est restore model_perc`depvar'_m1
			lincomest _b[la_clerk] 
			estadd scalar clerknoteth_majnoteth = r(estimate)
			estadd scalar clerknoteth_majnoteth_se = r(se)
			est store m1_lincom2
			* j is non co-ethnic with the clerk, but co-ethnic with the council
			est restore model_perc`depvar'_m1
			lincomest _b[majority] + _b[la_clerk] + _b[c.majority#c.la_clerk]
			estadd scalar clerknoteth_majeth = r(estimate)
			estadd scalar clerknoteth_majeth_se = r(se)
			est store m1_lincom3
			* j is co-ethnic with the clerk, but not with the council
			est restore model_perc`depvar'_m1
			lincomest _b[la_clerk] + _b[clerketh]
			estadd scalar clerketh_majnoteth = r(estimate)
			estadd scalar clerketh_majnoteth_se = r(se)
			est store m1_lincom4
			* j is co-ethnic with the clerk and the council 
			est restore model_perc`depvar'_m1
			lincomest _b[majority] + _b[la_clerk] + _b[c.majority#c.la_clerk] + _b[clerketh] + _b[c.majority#c.clerketh]
			estadd scalar clerketh_majeth = r(estimate)
			estadd scalar clerketh_majeth_se = r(se)
			* store other stats
			est store m1_lincom5
			gen used_d`depvar' = e(sample)
			quietly sum perc`depvar' if used_d`depvar' == 1
			estadd scalar outcome_mean = r(mean)
			estfe model_perc`depvar'_m1, labels(TERETHid "Locality-ethnicity FE" year "Year FE" ETHYEARid "Ethnicity-year FE" TERYEARid "Locality-year FE")
			drop used*
	
* model with ethnic group-locality, ethnic group-year, and locality-year fixed effects
quietly reghdfe perc`depvar' `indvar', absorb(TERETHid ETHYEARid TERYEARid) vce(cluster territory_level) // ethnic group-year and locality-year fixed effects 
		est store model_perc`depvar'_m2
		estadd local TERETH "Yes"
		estadd local ETHYEAR "Yes"
		estadd local year "No"
		estadd local TERYEAR "Yes"
		* calculate linear combinations and other stats 
			* no clerk and j is co-ethnic with the council
			est restore model_perc`depvar'_m2
			lincomest _b[majority] 
			estadd scalar noclerk_majeth = r(estimate)
			estadd scalar noclerk_majeth_se = r(se)
			est store m2_lincom1
			* j is non-coethnic with the clerk and the council
			est restore model_perc`depvar'_m2
			lincomest _b[la_clerk] 
			estadd scalar clerknoteth_majnoteth = r(estimate)
			estadd scalar clerknoteth_majnoteth_se = r(se)
			est store m2_lincom2
			* j is non co-ethnic with the clerk, but co-ethnic with the council
			est restore model_perc`depvar'_m2
			lincomest  _b[majority] + _b[la_clerk] + _b[c.majority#c.la_clerk]
			estadd scalar clerknoteth_majeth = r(estimate)
			estadd scalar clerknoteth_majeth_se = r(se)
			est store m2_lincom3
			* j is co-ethnic with the clerk, but not with the council
			est restore model_perc`depvar'_m2
			lincomest _b[la_clerk] + _b[clerketh]
			estadd scalar clerketh_majnoteth = r(estimate)
			estadd scalar clerketh_majnoteth_se = r(se)
			est store m2_lincom4
			* j is co-ethnic with the clerk and the council 
			est restore model_perc`depvar'_m2
			lincomest _b[majority] + _b[la_clerk] + _b[c.majority#c.la_clerk] + _b[clerketh] + _b[c.majority#c.clerketh]
			estadd scalar clerketh_majeth = r(estimate)
			estadd scalar clerketh_majeth_se = r(se)
			* store other stats
			est store m2_lincom5
			gen used_d`depvar' = e(sample)
			sum perc`depvar' if used_d`depvar' == 1
			estadd scalar outcome_mean = r(mean)
			estfe model_perc`depvar'_m2, labels(TERETHid "Locality-ethnicity FE" year "Year FE" ETHYEARid "Ethnicity-year FE" TERYEARid "Locality-year FE")
			drop used*
			
* create Table 3 
esttab model_perchired_m1 model_perchired_m2 using "table_3.tex", replace ///
	   booktabs b(3) se(3) eqlabels(none) alignment(c) ///
	   stats(outcome_mean N r2 ///
			 TERETH year ETHYEAR TERYEAR, ///
			 fmt(3 0 3) label("Outcome Mean" "Observations" "R-Squared" ///
				   "Locality-ethnicity FE" "Year FE" "Ethnicity-year FE" "Locality-year FE")) ///
	   collabels(,lhs(Outcome:)) ///
	   label mtitle("Hired" "Hired") nonumbers ///
	   varlabels(majority "Majority" la_clerk "Clerk presence" c.majority#c.la_clerk "Majority x Clerk presence" ///
				clerketh "Clerk ethnicity" c.majority#c.clerketh "Majority x Clerk ethnicity" ///
				perchiredmiss "Missing indicator" c.majority#c.perchiredmiss "Majority x Missing indicator" ///
				c.la_clerk#c.perchiredmiss "Clerk presence x Missing indicator" ///
				c.majority#c.la_clerk#c.perchiredmiss "Majority x Clerk presence x Missing indicator" ///
				c.clerketh#c.perchiredmiss "Clerk ethnicity x Missing indicator" ///
				c.majority#c.clerketh#c.perchiredmiss "Majority x Clerk ethnicity x Missing indicator") ///
	   keep(majority la_clerk c.majority#c.la_clerk clerketh c.majority#c.clerketh ///
			perchiredmiss c.majority#c.perchiredmiss c.la_clerk#c.perchiredmiss ///
			c.majority#c.la_clerk#c.perchiredmiss c.clerketh#c.perchiredmiss) ///
	   star(* 0.10 ** 0.05 *** 0.01)	   

/////
// Table 4: Hiring by salary groups.
/////

* open local authority-level data
use "localauthority_level.dta", clear 
est clear

* define dependent variables
local depvar hired1020 hired19
foreach i of local depvar {
	
* model with ethnic group-locality and year fixed effects 
local indvar "c.majority##c.la_clerk##c.clerketh##c.perc`i'miss" 
	quietly reghdfe perc`i' `indvar', absorb(TERETHid year) vce(cluster territory_level) // ethnic group-locality and year fixed effects 
		est store model_perc`i'_m1
		estadd local TERETH "Yes"
		estadd local ETHYEAR "No"
		estadd local year "Yes"
		estadd local TERYEAR "No"
		* calculate linear combinations and other stats 	
			* no clerk and j is non co-ethnic with the council
			summarize perc`i' if majority == 0 & la_clerk == 0 & clerketh == 0
			estadd scalar noclerk_majnoteth = r(mean)
			* no clerk and j is co-ethnic with the council
			est restore model_perc`i'_m1
			lincom _b[majority]
			estadd scalar noclerk_majeth = r(estimate)
			estadd scalar noclerk_majeth_se = r(se)
			* j is non-coethnic with the clerk and the council
			est restore model_perc`i'_m1
			lincom _b[la_clerk] 
			estadd scalar clerknoteth_majnoteth = r(estimate)
			estadd scalar clerknoteth_majnoteth_se = r(se)
			* j is non co-ethnic with the clerk, but co-ethnic with the council
			est restore model_perc`i'_m1
			lincom  _b[majority] + _b[la_clerk] + _b[c.majority#c.la_clerk]
			estadd scalar clerknoteth_majeth = r(estimate)
			estadd scalar clerknoteth_majeth_se = r(se)
			* j is co-ethnic with the clerk, but not with the council
			est restore model_perc`i'_m1
			lincom _b[la_clerk] + _b[clerketh]
			estadd scalar clerketh_majnoteth = r(estimate)
			estadd scalar clerketh_majnoteth_se = r(se)
			* j is co-ethnic with the clerk and the council 
			est restore model_perc`i'_m1
			lincom _b[majority] + _b[la_clerk] + _b[c.majority#c.la_clerk] + _b[clerketh] + _b[c.majority#c.clerketh]
			estadd scalar clerketh_majeth = r(estimate)
			estadd scalar clerketh_majeth_se = r(se)
			* store other stats
			gen used_d`i' = e(sample)
			sum perc`i' if used_d`i' == 1
			estadd scalar outcome_mean = r(mean)
			estfe model_perc`i'_m1, labels(TERETHid "LA-ethnicity FE" year "Year FE" ETHYEARid "Ethnicity-year FE" TERYEARid "LA-year FE")
			drop used*
			est restore model_perc`i'_m1
			nlcom (lincom1: _b[majority]) ///
				  (lincom2: _b[la_clerk]) ///
				  (lincom3: _b[majority] + _b[la_clerk] + _b[c.majority#c.la_clerk]) ///
				  (lincom4: _b[la_clerk] + _b[clerketh]) ///
				  (lincom5: _b[majority] + _b[la_clerk] + _b[c.majority#c.la_clerk] + _b[clerketh] + _b[c.majority#c.clerketh]), post
			est store perc`i'_m1
	
* model with ethnic group-locality, ethnic group-year, and locality-year fixed effects
local indvar "c.majority##c.la_clerk##c.clerketh##c.perc`i'miss" 
	quietly reghdfe perc`i' `indvar', absorb(TERETHid ETHYEARid TERYEARid) vce(cluster territory_level) // ethnic group-year and locality-year fixed effects 
		est store model_perc`i'_m2
		estadd local TERETH "Yes"
		estadd local ETHYEAR "Yes"
		estadd local year "No"
		estadd local TERYEAR "Yes"
		* calculate linear combinations and other stats 	
			* no clerk and j is non co-ethnic with the council
			summarize perc`i' if majority == 0 & la_clerk == 0 & clerketh == 0
			estadd scalar noclerk_majnoteth = r(mean)
			* no clerk and j is co-ethnic with the council
			est restore model_perc`i'_m2
			lincom _b[majority] 
			estadd scalar noclerk_majeth = r(estimate)
			estadd scalar noclerk_majeth_se = r(se)
			* j is non-coethnic with the clerk and the council
			est restore model_perc`i'_m2
			lincom _b[la_clerk] 
			estadd scalar clerknoteth_majnoteth = r(estimate)
			estadd scalar clerknoteth_majnoteth_se = r(se)
			* j is non co-ethnic with the clerk, but co-ethnic with the council
			est restore model_perc`i'_m2
			lincom  _b[majority] + _b[la_clerk] + _b[c.majority#c.la_clerk]
			estadd scalar clerknoteth_majeth = r(estimate)
			estadd scalar clerknoteth_majeth_se = r(se)
			* j is co-ethnic with the clerk, but not with the council
			est restore model_perc`i'_m2
			lincom _b[la_clerk] + _b[clerketh]
			estadd scalar clerketh_majnoteth = r(estimate)
			estadd scalar clerketh_majnoteth_se = r(se)
			* j is co-ethnic with the clerk and the council 
			est restore model_perc`i'_m2
			lincom _b[majority] + _b[la_clerk] + _b[c.majority#c.la_clerk] + _b[clerketh] + _b[c.majority#c.clerketh]
			estadd scalar clerketh_majeth = r(estimate)
			estadd scalar clerketh_majeth_se = r(se)
			* store other stats
			gen used_d`i' = e(sample)
			sum perc`i' if used_d`i' == 1
			estadd scalar outcome_mean = r(mean)
			estfe model_perc`i'_m2, labels(TERETHid "LA-ethnicity FE" year "Year FE" ETHYEARid "Ethnicity-year FE" TERYEARid "LA-year FE")
			drop used*
			* store other stats
			est restore model_perc`i'_m2
			nlcom (lincom1: _b[majority]) ///
				  (lincom2: _b[la_clerk]) ///
				  (lincom3: _b[majority] + _b[la_clerk] + _b[c.majority#c.la_clerk]) ///
				  (lincom4: _b[la_clerk] + _b[clerketh]) ///
				  (lincom5: _b[majority] + _b[la_clerk] + _b[c.majority#c.la_clerk] + _b[clerketh] + _b[c.majority#c.clerketh]), post
			est store perc`i'_m2
			
}

* create Table 4
esttab model_perchired19_m1 model_perchired19_m2 model_perchired1020_m1 model_perchired1020_m2 using "table_4.tex", replace ///
	   booktabs b(3) se(3) eqlabels(none) alignment(c) ///
	   stats(outcome_mean N r2 ///
			 TERETH year ETHYEAR TERYEAR, ///
			 fmt(3 0 3) label("Outcome Mean" "Observations" "R-Squared" ///
				   "Locality-ethnicity FE" "Year FE" "Ethnicity-year FE" "Locality-year FE")) ///
	   collabels(,lhs(Outcome:)) ///
	   label mtitle("Hired (1-9)" "Hired (1-9)" "Hired (10-20)" "Hired (10-20)") nonumbers ///
	   varlabels(majority "Majority" la_clerk "Clerk presence" c.majority#c.la_clerk "Majority x Clerk presence" ///
				clerketh "Clerk ethnicity" c.majority#c.clerketh "Majority x Clerk ethnicity" ///
				perchired19miss "Missing indicator (1-9)" c.majority#c.perchired19miss "Majority x Missing indicator (1-9)" ///
				c.la_clerk#c.perchired19miss "Clerk presence x Missing indicator (1-9)" ///
				c.majority#c.la_clerk#c.perchired19miss "Majority x Clerk presence x Missing indicator (1-9)" ///
				c.clerketh#c.perchired19miss "Clerk ethnicity x Missing indicator (1-9)" ///
				c.majority#c.clerketh#c.perchired19miss "Majority x Clerk ethnicity x Missing indicator (1-9)" ///
				perchired1020miss "Missing indicator (10-20)" c.majority#c.perchired1020miss "Majority x Missing indicator (10-20)" ///
				c.la_clerk#c.perchired1020miss "Clerk presence x Missing indicator (10-20)" ///
				c.majority#c.la_clerk#c.perchired1020miss "Majority x Clerk presence x Missing indicator (10-20)" ///
				c.clerketh#c.perchired1020miss "Clerk ethnicity x Missing indicator (10-20)" ///
				c.majority#c.clerketh#c.perchired1020miss "Majority x Clerk ethnicity x Missing indicator (10-20)") ///
	   keep(majority la_clerk c.majority#c.la_clerk clerketh c.majority#c.clerketh ///
			perchired19miss c.majority#c.perchired19miss c.la_clerk#c.perchired19miss ///
			c.majority#c.la_clerk#c.perchired19miss c.clerketh#c.perchired19miss ///
			perchired1020miss c.majority#c.perchired1020miss c.la_clerk#c.perchired1020miss ///
			c.majority#c.la_clerk#c.perchired1020miss c.clerketh#c.perchired1020miss) ///
	   star(* 0.10 ** 0.05 *** 0.01)	


